library(readxl)
library(plyr)
library(dplyr)
library(lme4)
library(ggplot2)
library(ggmosaic)
library(cowplot)
library(patchwork)

setwd ("[INSERT WORKING DIRECTORY HERE]")


#### Figure1
statall <- data.frame(
  base=c(1,2,3,4,5),
  rev=c(1.68,2.57,3.16,3.83,4.43)
)

ggplot() +
  geom_point(data = statall, aes(x = base, y = rev, color = "Revenue use"), size = 3)+
  geom_segment(data = statall |> filter(base %in% c(4, 5)), 
               aes(x = base, y = base, xend = base, yend = rev+.05, linetype = "Shift"), 
               arrow = arrow(length = unit(0.2, "cm")), color = "gray10", size = .5) +
  geom_segment(data = statall |> filter(base %in% c(1, 2, 3)), 
               aes(x = base, y = base, xend = base, yend = rev-.05, linetype = "Shift"), 
               arrow = arrow(length = unit(0.2, "cm")), color = "gray10", size = .5) +
geom_segment(data=statall, aes(x = base - 0.25, xend = base + 0.25, y = base, yend = base, color = "Baseline"), size = 3)+
geom_hline(aes(yintercept = 3, linetype = "Neither in Favor nor Against"), color = "black")+ 
  labs(title = "Within subject comparison of acceptability for wind power deployment", 
       x = "Base acceptability for wind power without compensation", 
       y = "Average acceptability for wind power with compensation",
       color = "Mean",  
       linetype = "") + 
  scale_x_continuous(breaks = 1:5, 
                     labels = c("Strongly against", "Somewhat against", 
                                "Neither in favor nor against", "Somewhat in favor", "Strongly in favor")) +
  scale_color_manual(values = c("Baseline" = "gray", "Revenue use" = "gray10", "Public Goods"= "gray40", "Tax Cuts"= "gray70")) +
  scale_linetype_manual(values = c("Shift" = "solid", "Neither in favor nor against" = "dashed")) +
  theme_bw() +
  theme(legend.position = "under")



#### Figure2
usa <- data.frame(
  base=c(1,2,3,4,5),
  rev=c(1.44,2.55,3.13,3.83,4.53)
)

safrica <- data.frame(
  base=c(1,2,3,4,5),
  rev=c(2.06,2.59,3.30,3.95,4.56)
)

india <- data.frame(
  base=c(1,2,3,4,5),
  rev=c(1.99,2.51,3.11,3.79,4.52)
)

germany <- data.frame(
  base=c(1,2,3,4,5),
  rev=c(1.55,2.48,3.09,3.69,3.96)
)

brazil <- data.frame(
  base=c(1,2,3,4,5),
  rev=c(2.26,2.80,3.25,3.89,4.53)
)

## Brazil
plot1 <- ggplot() +
  geom_hline(yintercept = 3, linetype = "dashed", color = "black") + 
  geom_point(data = brazil, aes(x = base, y = rev), color = "gray10", size = 3) +
  geom_segment(data = brazil, aes(x = base, xend = base, y = base, yend = rev), 
               arrow = arrow(length = unit(0.2, "cm")), color = "gray30", size = 0.5) +
  geom_segment(data = brazil, aes(x = base - 0.25, xend = base + 0.25, y = base, yend = base), 
               color = "gray70", size = 3) +
  labs(title = "Brazil", 
       y = "Average acceptability for wind power with compensation") + 
  scale_x_continuous(breaks = 1:5, 
                     labels = c("Str. Ag.", "Some. Ag.", "in Fa. nor Ag.", "Some. in Fa.", "Str. in Fa.")) +
  theme_bw() +
  theme(axis.title.y = element_blank(),
        axis.title.x = element_blank(),  
        axis.text.x = element_blank(),
        legend.position = "none")

## Germany
plot2 <- ggplot() +
  geom_hline(yintercept = 3, linetype = "dashed", color = "black") + 
  geom_point(data = germany, aes(x = base, y = rev), color = "gray10", size = 3) +
  geom_segment(data = germany, aes(x = base, xend = base, y = base, yend = rev), 
               arrow = arrow(length = unit(0.2, "cm")), color = "gray30", size = 0.5) +
  geom_segment(data = germany, aes(x = base - 0.25, xend = base + 0.25, y = base, yend = base), 
               color = "gray70", size = 3) +
  labs(title = "Germany") +
  scale_x_continuous(breaks = 1:5, 
                     labels = c("Str. Ag.", "Some. Ag.", "in Fa. nor Ag.", "Some. in Fa.", "Str. in Fa.")) +
  theme_bw() +
  theme(axis.title.y = element_blank(),
        axis.title.x = element_blank(),  
        axis.text.x = element_blank(),
        legend.position = "none")

## India
plot3 <- ggplot() +
  geom_hline(yintercept = 3, linetype = "dashed", color = "black") + 
  geom_point(data = india, aes(x = base, y = rev), color = "gray10", size = 3) +
  geom_segment(data = india, aes(x = base, xend = base, y = base, yend = rev), 
               arrow = arrow(length = unit(0.2, "cm")), color = "gray30", size = 0.5) +
  geom_segment(data = india, aes(x = base - 0.25, xend = base + 0.25, y = base, yend = base), 
               color = "gray70", size = 3) +
  labs(title = "India") +
  scale_x_continuous(breaks = 1:5, 
                     labels = c("Str. Ag.", "Some. Ag.", "in Fa. nor Ag.", "Some. in Fa.", "Str. in Fa.")) +
  theme_bw() +
  theme(axis.title.y = element_blank(),
        axis.title.x = element_blank(),  
        axis.text.x = element_blank(),
        legend.position = "none")

## South Africa
plot4 <- ggplot() +
  geom_hline(yintercept = 3, linetype = "dashed", color = "black") + 
  geom_point(data = safrica, aes(x = base, y = rev), color = "gray10", size = 3) +
  geom_segment(data = safrica, aes(x = base, xend = base, y = base, yend = rev), 
               arrow = arrow(length = unit(0.2, "cm")), color = "gray30", size = 0.5) +
  geom_segment(data = safrica, aes(x = base - 0.25, xend = base + 0.25, y = base, yend = base), 
               color = "gray70", size = 3) +
  labs(title = "South Africa", 
       x = "Base acceptability for wind power without compensation", 
       y = "Average acceptability for wind power with compensation") +
  scale_x_continuous(breaks = 1:5, 
                     labels = c("Str. Ag.", "Some. Ag.", "in Fa. nor Ag.", "Some. in Fa.", "Str. in Fa.")) +
  theme_bw() +
  theme(axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        legend.position = "none")

## USA
plot5 <- ggplot() +
  geom_hline(yintercept = 3, linetype = "dashed", color = "black") + 
  geom_point(data = usa, aes(x = base, y = rev), color = "gray10", size = 3) +
  geom_segment(data = usa, aes(x = base, xend = base, y = base, yend = rev), 
               arrow = arrow(length = unit(0.2, "cm")), color = "gray30", size = 0.5) +
  geom_segment(data = usa, aes(x = base - 0.25, xend = base + 0.25, y = base, yend = base), 
               color = "gray70", size = 3) +
  labs(title = "USA",
       x = "Base acceptability for wind power without compensation", 
       y = "Average acceptability for wind power with compensation") +
  scale_x_continuous(breaks = 1:5, 
                     labels = c("Str. Ag.", "Some. Ag.", "in Fa. nor Ag.", "Some. in Fa.", "Str. in Fa.")) +
  theme_bw() +
  theme(axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        legend.position = "none")

## All countries
plot6 <- ggplot() +
  geom_hline(yintercept = 3, linetype = "dashed", color = "black") + 
  geom_point(data = statall, aes(x = base, y = rev), color = "gray10", size = 3) +
  geom_segment(data = statall, aes(x = base, xend = base, y = base, yend = rev), 
               arrow = arrow(length = unit(0.2, "cm")), color = "gray30", size = 0.5) +
  geom_segment(data = statall, aes(x = base - 0.25, xend = base + 0.25, y = base, yend = base), 
               color = "gray70", size = 3) +
  labs(title = "All countries",
       x = "Base acceptability for wind power without compensation", 
       y = "Average acceptability for wind power with compensation") +
  scale_x_continuous(breaks = 1:5, 
                     labels = c("Str. Ag.", "Some. Ag.", "in Fa. nor Ag.", "Some. in Fa.", "Str. in Fa.")) +
  theme_bw() +
  theme(axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        legend.position = "none")

######### Create titles
# Y-axis title empty plot
y_axis_title <- ggplot() + 
  theme_void() +
  labs(y = "Average acceptability for wind power deployment with compensation") +
  theme(
    plot.margin = margin(r = -20),
    axis.title.y = element_text(angle = 90, size = 11, vjust = 0.5)
  )

# X-axis title empty plot
x_axis_title <- ggplot() + 
  theme_void() +
  labs(x = "Base acceptability for wind power deployment without compensation") +
  theme(
    plot.margin = margin(t = -10),
    axis.title.x = element_text(size = 11, vjust = 1)
  )

########### Combine plots
full_plot <- y_axis_title + 
  ((plot1 + plot2 + plot3) / (plot4 + plot5 + plot6)) + 
  plot_layout(widths = c(1, 100))  # 1 for y-title, 10 for rest

# Add x-axis title below
full_plot / x_axis_title + plot_layout(heights = c(100, 1))  # 10 for plots, 1 for x-title



